package Dao;

import Entity.Officer;
import Entity.Patient;
import Entity.Room;
import Utils.SimpleTools;
import javafx.scene.control.Alert;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PatientDao {
    //添加用户
    public static void add(String[] p) throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        boolean i = true;
        Patient patient = new Patient(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
        for(Patient j:list){
            int a = 0;
            if(j.getContact().equals(p[0])){
                SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "该用户已存在");
                i = false;
                break;
            }
            if(j.getName().equals(p[0])){
                patient.setName(patient.getName()+a++);
                SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "该姓名已存在", "姓名储存"+patient.getName());
            }
        }
        if(i){
            list.add(patient);
            om.writeValue(file,list);
        }
    }

    //根据字符串查询用户
    public static List<Patient> search(String str) throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        List<Patient> patients = new ArrayList<>();
        for(Patient patient:list){
            for(String s:patient.properties()){
                if(s!=null&&s.length()>=str.length()){
                    if(s.contains(str)){
                        patients.add(patient);
                        break;
                    }
                }
            }
        }
        return patients;
    }

    //返回没有房间或房间名不规范的用户的姓名集合
    public static List<String> noRoomPatientNames() throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        boolean tmp;
        List<String> pattients = new ArrayList<>();
        List<String> roomStr = StaffDao.getRoomstrs();
        for (Patient patient:list){
            tmp = true;
            if(patient.getRoom()==null){
                pattients.add(patient.getName());
                continue;
            }
            for(String room:roomStr){
                if(patient.getRoom().equals(room.toString())){
                    tmp = false;
                    break;
                }
            }
            if(tmp) pattients.add(patient.getName());
        }
        return pattients;
    }
    //根据房间返回用户的集合
    public static List<Patient> byRoom(Room room) throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        List<Patient> patients = new ArrayList<>();
        for (Patient patient:list){
            if(room.toString().equals(patient.getRoom())) patients.add(patient);
        }
        return patients;
    }
    //根据房间的集合返回用户的集合
    public static List<Patient> byRooms(List<Room> rooms) throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        List<Patient> patients = new ArrayList<>();
        for (Patient patient:list){
            for(Room room:rooms) if(room.toString().equals(patient.getRoom())) patients.add(patient);
        }
        return patients;
    }

}
